﻿using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Quickly.Identity.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quickly.Identity.Services.Manage;

public record QueryRolesCommand(Paging Paging);

public class QueryRoles
{
    public static async Task<PagedList<TRole>> ExecuteAsync<TRole>(
        QueryRolesCommand command,
        IQueryableRoleStore<TRole> queryableRoleStore,
        CancellationToken cancellationToken)
        where TRole : class
    {
        cancellationToken.ThrowIfCancellationRequested();

        var totalCount = await queryableRoleStore.Roles.CountAsync(cancellationToken);
        var roles = new PagedList<TRole>(command.Paging, totalCount);
        roles.Datas.AddRange(await queryableRoleStore.Roles.TakePage(command.Paging).AsNoTracking().ToListAsync(cancellationToken));
        return roles;
    }
}
